package com.cskaoyan.app.wxservice.impl;

import com.cskaoyan.app.bean.po.*;
import com.cskaoyan.app.bean.vo.wx.WxHomeIndexFloorVO;
import com.cskaoyan.app.bean.vo.wx.WxHomeIndexVO;
import com.cskaoyan.app.mapper.*;
import com.cskaoyan.app.wxservice.WxHomeService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxHomeServiceImpl implements WxHomeService {

    @Autowired
    private MarketGoodsMapper goodsMapper;

    @Autowired
    private MarketCouponMapper couponMapper;

    @Autowired
    private MarketAdMapper adMapper;

    @Autowired
    private MarketBrandMapper brandMapper;

    @Autowired
    private MarketTopicMapper topicMapper;

    @Autowired
    private MarketCategoryMapper categoryMapper;

    @Autowired
    private MarketSystemMapper systemMapper;

    @Override
    public WxHomeIndexVO index() {
        MarketSystemExample systemExample = new MarketSystemExample();
        systemExample.createCriteria().andKeyNameLike("%market_wx%");
        List<MarketSystem> systemList = systemMapper.selectByExample(systemExample);
        HashMap<String, Integer> map = new HashMap<>();
        for (MarketSystem system : systemList) {
            String key = system.getKeyName();
            String value = system.getKeyValue();
            if ("true".equals(value) || "false".equals(value)) {
                continue;
            }
            int number = Integer.parseInt(value);
            map.put(key, number);
        }

        MarketAdExample adExample = new MarketAdExample();
        adExample.createCriteria().andDeletedEqualTo(false);
        List<MarketAd> banner = adMapper.selectByExample(adExample);

        MarketCategoryExample categoryExample = new MarketCategoryExample();
        categoryExample.createCriteria().andLevelEqualTo("L1").andDeletedEqualTo(false);
        List<MarketCategory> channel = categoryMapper.selectByExample(categoryExample);

        MarketGoodsExample newGoodsExample = new MarketGoodsExample();
        newGoodsExample.createCriteria().andIsNewEqualTo(true).andDeletedEqualTo(false);
        PageHelper.startPage(1, map.get("market_wx_index_new"));
        List<MarketGoods> newGoodsList = goodsMapper.selectByExampleWithBLOBs(newGoodsExample);
        PageHelper.clearPage();

        MarketGoodsExample hotGoodsExample = new MarketGoodsExample();
        hotGoodsExample.createCriteria().andIsHotEqualTo(true).andDeletedEqualTo(false);
        PageHelper.startPage(1, map.get("market_wx_index_hot"));
        List<MarketGoods> hotGoodsList = goodsMapper.selectByExampleWithBLOBs(hotGoodsExample);
        PageHelper.clearPage();

        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.createCriteria().andDeletedEqualTo(false).andStatusEqualTo((short) 0);
        PageHelper.startPage(1, 3);
        List<MarketCoupon> couponList = couponMapper.selectByExample(couponExample);
        PageHelper.clearPage();

        MarketBrandExample brandExample = new MarketBrandExample();
        brandExample.createCriteria().andDeletedEqualTo(false);
        PageHelper.startPage(1, map.get("market_wx_index_brand"));
        List<MarketBrand> brandList = brandMapper.selectByExample(brandExample);
        PageHelper.clearPage();

        MarketTopicExample topicExample = new MarketTopicExample();
        topicExample.createCriteria().andDeletedEqualTo(false);
        PageHelper.startPage(1, map.get("market_wx_index_topic"));
        List<MarketTopic> topicList = topicMapper.selectByExample(topicExample);
        PageHelper.clearPage();

        PageHelper.startPage(1, map.get("market_wx_catlog_list"));
        List<MarketCategory> categoryListL1 = categoryMapper.selectByExample(categoryExample);
        PageHelper.clearPage();

        List<WxHomeIndexFloorVO> floorGoodsList = new ArrayList<>();
        for (MarketCategory categoryL1 : categoryListL1) {
            MarketCategoryExample example = new MarketCategoryExample();
            example.createCriteria().andPidEqualTo(categoryL1.getId()).andDeletedEqualTo(false);
            List<MarketCategory> categoryListL2 = categoryMapper.selectByExample(example);

            List<Integer> categoryL2Id = new ArrayList<>();
            for (MarketCategory categoryL2 : categoryListL2) {
                categoryL2Id.add(categoryL2.getId());
            }

            MarketGoodsExample goodsExample = new MarketGoodsExample();
            goodsExample.createCriteria().andCategoryIdIn(categoryL2Id).andDeletedEqualTo(false);
            PageHelper.startPage(1, map.get("market_wx_catlog_goods"));
            List<MarketGoods> goodsList = goodsMapper.selectByExampleWithBLOBs(goodsExample);
            PageHelper.clearPage();
            floorGoodsList.add(new WxHomeIndexFloorVO(categoryL1.getName(), goodsList, categoryL1.getId()));
        }

        return new WxHomeIndexVO(newGoodsList, couponList, banner, brandList, hotGoodsList, topicList, channel, floorGoodsList);
    }

    @Override
    public Map<String, String> about() {
        Map<String, String> props = new HashMap<>();
        Map<String, String> about = new HashMap<>();
        props.put("market_mall_name", "name");
        props.put("market_mall_address", "address");
        props.put("market_mall_longitude", "longitude");
        props.put("market_mall_latitude", "latitude");
        props.put("market_mall_phone", "phone");
        props.put("market_mall_qq", "qq");
        MarketSystemExample example = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = example.createCriteria();
        criteria.andKeyNameIn(new ArrayList<>(props.keySet()))
                        .andDeletedEqualTo(false);
        List<MarketSystem> propList = systemMapper.selectByExample(example);
        propList.forEach(prop -> about.put(props.get(prop.getKeyName()), prop.getKeyValue()));
        return about;
    }
}
